require("dotenv").config();
const { Client, GatewayIntentBits,EmbedBuilder } = require('discord.js');
const client = new Client({ intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.GuildMessages,
] });

let recordChannel = null

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});
    
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === 'ping') {
    await interaction.reply('Pong!');
}
});

client.on('messageCreate', async (message) => {
    if (message.author.bot) return;
    // console.log(message.author)
    console.log(message.content)
    const args = message.content.split(/\s+/);
    for (let index = 0; index < args.length; index++) {
        const element = args[index];
        console.log("element is ",element)
        if(element === "party"){
            console.log("111")
            recordChannel = message.channel
            return message.reply("记录")
        }
        
    }
});


client.login(process.env.DISCORDJS_BOT_TOKEN).then( message => {
    console.log(`login sucuss: ${message}`)
    
})
.catch(message => {
    console.log(`login fiald: ${message}`)
    
})



const PROTO_PATH = __dirname + '/protos/helloworld.proto';
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');


var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;


function sayHello(call, callback) {
    console.log("sayHello")
    if(recordChannel){
        // inside a command, event listener, etc.
        let exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle('Aaron Woods is inviting you to a party!')
        .setURL('http://45.32.32.246:3000/links/comesocial?tempLink='+call.request.name)
        // .setAuthor({ name: 'Author Name', iconURL: 'https://i.imgur.com/AfFp7pu.png', url: 'https://discord.js.org' })
        // .setDescription('Some description here')
        // .setThumbnail('https://i.imgur.com/AfFp7pu.png')
        // .addFields(
        //     { name: 'Regular field title', value: 'Some value here' },
        //     { name: '\u200B', value: '\u200B' },
        //     { name: 'Inline field title', value: 'Some value here', inline: true },
        //     { name: 'Inline field title', value: 'Some value here', inline: true },
        // )
        // .addFields({ name: 'Inline field title', value: 'Some value here', inline: true })
        .setImage('http://45.32.32.246:3000/res/img/discord_bg.jpg');
        // .setTimestamp()
        // .setFooter({ text: 'Some footer text here', iconURL: 'https://i.imgur.com/AfFp7pu.png' });


        recordChannel.send({ embeds: [exampleEmbed] })
        .then(message => {
            // console.log(`Sent message: ${message.content}`)
            callback(null, {message: 'Hello ' + call.request.name});
        })
        .catch(message => {
            callback(null, {message: 'Hello ' + call.request.name});
        })
    }else{
        console.log("recordChannel is nil")
        callback(null, {message: 'Hello ' + call.request.name});
    }
    
}

var server = new grpc.Server();
server.addService(hello_proto.Greeter.service, {sayHello: sayHello});
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
    console.log("bindAsync")
    server.start();
});